package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.QuarterlyShareDataDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.QuarterlyShareData;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class QuarterlyShareDataDaoImpl extends
		AbstractDaoImpl<QuarterlyShareData, Integer> implements
		QuarterlyShareDataDao {

	protected QuarterlyShareDataDaoImpl() {
		super(QuarterlyShareData.class);
	}

	@Override
	public void saveQuarterlyShareData(QuarterlyShareData quarterlyShareData) {
		saveOrUpdate(quarterlyShareData);
	}

	@Override
	public List<QuarterlyShareData> findQuarterlyShareData(int companyId) {
		return findByCriteria(Restrictions.eq("companyId", companyId));
	}

	@Override
	public List<QuarterlyShareData> searchQuarterlyShareData(int companyId,
			int fromDate, int toDate, String quarterCode, String orderBy,
			String orderByName) {
		Criteria criteria = getCurrentSession().createCriteria(
				QuarterlyShareData.class);

		criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != 0) {
			criteria.add(Restrictions.ilike("quarterCode", fromDate));
		}

		if (toDate != 0) {
			criteria.add(Restrictions.ilike("quarterCode", toDate));
		}
		if (quarterCode != null) {
			criteria.add(Restrictions.ilike("quarterCode", quarterCode));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<QuarterlyShareData> searchQuarterlyShareData(int companyId,
			String companyName, int fromDate, int toDate, String quarterCode,
			String orderBy, String orderByName, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				QuarterlyShareData.class);

		if (orderBy != null) {
			criteria.addOrder(Order.desc(getRecordOrder(orderBy)));
			if (orderByName != null) {
				criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
			}
		} else {
			criteria.addOrder(Order.desc("date"));

			criteria.createAlias("company", "cmp");
			criteria.addOrder(Order.asc("cmp.code"));
		}

		if (companyId > 0) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (companyName != null) {
			criteria.createAlias("company", "comp");
			criteria.add(Restrictions.like("comp.name", companyName));
		}
		if (fromDate > 0) {
			criteria.add(Restrictions.ilike("quarterCode", fromDate));
		}

		if (toDate > 0) {
			criteria.add(Restrictions.ilike("quarterCode", toDate));
		}
		if (quarterCode != null) {
			criteria.add(Restrictions.ilike("quarterCode",
					("%" + quarterCode + "%")));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

	@Override
	@Transactional
	public void deleteQuarterlyShareData(int companyId, int fromYear,
			int toYear, String quarterCode) {

		String sql = "delete from QuarterlyShareData where 1=1 ";
		if (companyId > Constants.ERROR) {
			sql += "and company_id = :companyId ";
		}
		if (fromYear > 0) {
			sql += "and year >= :fromYear ";
		}
		if (toYear > 0) {
			sql += "and year <= :toYear ";
		}
		if (quarterCode != "") {
			sql += "and quarter_code = :quarterCode ";
		}

		Query query = getCurrentSession().createQuery(sql);
		if (companyId > Constants.ERROR) {
			query.setInteger("companyId", companyId);
		}
		if (fromYear > 0) {
			query.setParameter("fromYear", fromYear);
		}
		if (toYear > 0) {
			query.setParameter("toYear", toYear);
		}
		if (quarterCode != "") {
			query.setParameter("quarterCode", quarterCode);
		}

		query.executeUpdate();

	}

}
